/*
 * Users_LastDefine.h
 *
 *  Created on: 2021年4月7日
 *      Author: MrWang
 */

#ifndef USERS_LASTDEFINE_H_
#define USERS_LASTDEFINE_H_
#include "main.h"
#include <rtthread.h>

//为了保证系统运行,要求系统必须存在的宏定义


#ifndef false
    # define false   0x00
#endif

#ifndef true
    # define true    0x01
#endif

#ifndef Users_valid
    #define Users_valid         (0xA5)
#endif
#ifndef Users_invalid
    #define Users_invalid       (0x00)
#endif

//#ifndef Users_LogOut
//#define Users_LogOut(NAME, fmt, ...)
//#endif

//#ifndef Users_Debug_LogOut
//#define Users_Debug_LogOut(NAME, fmt, ...)
//#endif

#ifdef DonotUseIWDG
#define	ClrWdt			__nop
#endif

extern rt_err_t rt_thread_sleep(rt_tick_t tick);

/****************************************************************************
* 函数：
* 功能：尝试获取用户自定义信号量
* 参数：
* 备注：输入参数分别为信号量,超时时间,返回的结果
*****************************************************************************/
#define Users_TakeSem_Ret(sem, time, false)	\
	do{\
		register rt_base_t temp;\
		uint32_t nowTick = 0;\
		nowTick = HAL_GetTick();\
		Retry:\
		temp = rt_hw_interrupt_disable();\
		if((sem) == false){\
			(sem) = true;\
			rt_hw_interrupt_enable(temp);\
		}\
		else{\
			rt_hw_interrupt_enable(temp);\
			if(HAL_GetTick() - nowTick > (time)){\
				(sem) = false;\
				return (false);\
			}\
			rt_thread_sleep(5);\
			goto Retry;\
		}\
	}while(0)

/****************************************************************************
* 函数：
* 功能：尝试获取用户自定义信号量
* 参数：
* 备注：输入参数分别为信号量,超时时间,返回的结果
*****************************************************************************/
#define Users_TakeSem(sem, time)	\
	do{\
		register rt_base_t temp;\
		uint32_t nowTick = 0;\
		nowTick = HAL_GetTick();\
		Retry:\
		temp = rt_hw_interrupt_disable();\
		if((sem) == false){\
			(sem) = true;\
			rt_hw_interrupt_enable(temp);\
		}\
		else{\
			rt_hw_interrupt_enable(temp);\
			if(HAL_GetTick() - nowTick > (time)){\
				(sem) = false;\
				return;\
			}\
			rt_thread_sleep(5);\
			goto Retry;\
		}\
	}while(0)


#endif /* USERS_LASTDEFINE_H_ */
